<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1000px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="110px" :disabled="disableEditSubmit">
      <el-row>
				<el-col :span="12">
					<el-form-item label="合同名称" prop="contractname" >
						<el-input v-model="model.contractname" :disabled="true"></el-input>
					</el-form-item>
				</el-col>
				<!-- <el-col :span="12">
					<el-form-item label="合同编号" prop="contractcode" >
						<el-input v-model="model.contractcode" :disabled="true"></el-input>
					</el-form-item>
				</el-col> -->
      </el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="离职/转岗人员" prop="oldapplicantcode" :rules="rules.validateRequire">
							<tl-ref-user v-model="model.oldapplicantcode" :initDisplayData.sync="model.oldapplicant" dataType="code" :singleSelect="true" :disabled="true"></tl-ref-user>
					</el-form-item>
				</el-col>
        <el-col :span="12">
					<el-form-item label="交接人" prop="newapplicantcode" :rules="rules.validateRequire">
						<tl-ref-user v-model="model.newapplicantcode" :initDisplayData.sync="model.newapplicant" dataType="code" :singleSelect="true" @confirm="confirm" ></tl-ref-user>
					</el-form-item>
				</el-col>
      </el-row>
			<el-row>
			</el-row>
			<el-row>
				<el-col :span="24">
					<el-form-item label="交接原因" prop="description" :rules="rules.validateRequire">
						<el-input v-model="model.description" type="textarea" :rows="4"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
      <el-row>
				<el-col :span="12">
					<el-form-item label="申请人" prop="usercode" >
						<tl-ref-user v-model="model.usercode" :initDisplayData.sync="model.username" dataType="code" :singleSelect="true" :disabled="true"></tl-ref-user>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="提交日期" prop="submitdate" >
						<el-date-picker v-model="model.submitdate" type="date" placeholder="提交日期" value-format="yyyy-MM-dd" :disabled="true"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
    </el-form>
    <tl-approval-history ref="history" sourceType="PmContractApplicantChange" :sourceId="model.id" />
    <history ref="indexHistory" :projectid="this.projectid" ></history>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disableSave"><svg-icon icon-class="save" />保存</el-button>
      <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval"><svg-icon icon-class="send" />提交审批</el-button>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory"><svg-icon icon-class="list" />审批历史</el-button>
      <el-button type="primary" @click="showHistoryIndex"><svg-icon icon-class="list" />历史交接</el-button>
      <!-- <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableEditSubmit" :disabled="disableDelApproval"><svg-icon icon-class="undo" />取消审批</el-button> -->
    </div>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import history from './indexHistory'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { history },
  data() {
    return {
      moduleTitle: '离职/转岗人员工作交接',
      urlPrefix: '/pm/contractApplicantChange/',
      url: {
        // 额外的url
        queryDeptcode: '/pm/contractApplicantChange/queryDeptcode'
      },
      oldapplicantcode: '',
      oldapplicant: '',
      contractname: '',
      projectid: '',
      deptid: ''
    }
  },
  methods: {
    confirm() {
      let data = this.model.newapplicantcode
      if (data !== null && data !== undefined && data !== '') {
        const param = { deptcode: this.dept.code, usercode: this.model.newapplicantcode }
        getAction(this.url.queryDeptcode, param).then((res) => {
          if (res.success) {
          } else {
            this.$set(this.model, 'newapplicantcode', null)
            this.$set(this.model, 'newapplicant', null)
            this.$tap.alert(res.message, 'warning')
          }
        })
      }
    },
    // 打开历史交接
    showHistoryIndex() {
       this.$refs.indexHistory.visible = true
    },
    assignDefaultValue() {
      if (this.isAdd) {
        if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
          this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        }
        this.$set(this.model, 'orgcode', this.org.code)
        this.$set(this.model, 'disporgcode', this.org.name)
        this.$set(this.model, 'deptcode', this.dept.code)
        this.$set(this.model, 'deptname', this.dept.fullname)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        // 提交日期
        this.$set(this.model, 'submitdate', new Date())
        // 合同名称
        this.$set(this.model, 'contractname', this.contractname)
        // 原申请人工号
        this.$set(this.model, 'oldapplicantcode', this.oldapplicantcode)
        // 原申请人姓名
        this.$set(this.model, 'oldapplicant', this.oldapplicant)
        // 来源单据id
        this.$set(this.model, 'projectid', this.projectid)
        // 清空
        this.contractname = ''
        this.oldapplicantcode = ''
        this.oldapplicant = ''
        // this.projectid = ''
      }
    },
    loaded() {
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
    },
    doSubmitApproval() {
      if (!this.url.submit) {
        this.$tap.alert('请设置审批地址!', 'warning')
        return
      }
      this.$refs.form.validate((valid) => {
         if (valid) {
      this.submitLoading = true
      this.$tap
        .confirm('确定提交审批吗？提交后，单据内容不可修改！')
        .then((_) => {
          if (!this.beforeApproval()) {
            this.submitLoading = false
            return
          }
          putAction(this.url.submit, this.model).then((res) => {
            if (res.success) {
              this.$tap.message(res.message, 'success')
              this.queryData(this.model.id)
              this.afterApproval()
            } else {
              this.$tap.alert(res.message, 'warning')
            }
            this.submitLoading = false
          })
        })
        .catch((_) => {
          this.submitLoading = false
        })
        this.disableEditSubmit = true
         } else {
          return false
        }
      })
    }
  }
}
</script>

